const app = getApp();
const api = require("../../utils/api");

Page({
    data: {
        type: 0, //0 切换店铺 1 选择提货店铺
        shopId: '',
        storeList: [],
        storeSelected: '', //已选店铺

        pageNum: 1,
        pageSize: 8,
        total: 0,
        noContent: false, //无数据
        isFinished: false, //加载结束
    },
    onLoad(options) {
        this.getStoreData(); //获取店铺列表

        //保存店铺id
        let shopId = options.dir ? options.shopId : wx.getStorageSync('shopId')
        this.setData({
            shopId: parseInt(shopId),
            type: options.dir ? options.dir : 0,
        });
        if(this.data.shopId) {
            this.getStoreInfo();
        }
    },
    onShow() {
        
    },

    getStoreData() { //获取店铺列表
        let params = {
            shopName: '',
            pageNum: this.data.pageNum,
            pageSize: this.data.pageSize
        }
        api.post('/shop/page', params)
            .then(res => {
                let list = res.content;
                list.forEach(item => {
                    if(item.id == this.data.shopId) {
                        item.selected = true;
                    } else {
                        item.selected = false;
                    }
                    //整理营业时间
                    let businessBeginTime = item.businessBeginTime.split(':');
                    let businessEndTime = item.businessEndTime.split(':');
                    item.businessTime = businessBeginTime[0] + ':' + businessBeginTime[1] + '~' + businessEndTime[0] + ':' + businessEndTime[1];
                });

                let newList = [...this.data.storeList, ...list];
                this.setData({
                    storeList: newList,
                    total: res.total
                });

                //是否无内容标识
                if(this.data.storeList.length == 0) {
                    this.setData({
                        noContent: true
                    })
                } else {
                    this.setData({
                        noContent: false
                    });
                }
                //是否加载完毕标识
                if(this.data.storeList.length >= this.data.total) {
                    this.setData({
                        isFinished: true
                    })
                } else {
                    this.setData({
                        isFinished: false
                    })
                }
                wx.stopPullDownRefresh();
            })
    },
    getStoreInfo() { //获取店铺信息
        api.post('/shop/detail?shopId=' + this.data.shopId)
            .then(res => {
                this.setData({
                    storeSelected: res,
                });
            })
    },
    chooseStore(e) { //选择店铺
        let index = e.currentTarget.dataset.index;
        let list = this.data.storeList;
        list.forEach((item, i) => {
            if (i == index) {
                item.selected = true;
            } else {
                item.selected = false;
            }
        });
        this.setData({
            storeList: list
        });
        
        if(this.data.type == 1) { //选择提货店铺
            wx.setStorageSync('pickup', this.data.storeList[index].id); //保存店铺id
            app.globalData.isRefreshPickipStore = true; //刷新店铺标识

            wx.showToast({
                title: '店铺选择成功！',
                icon: 'success'
            });
            setTimeout(() => {
                wx.navigateBack(-1);
            }, 1000);
        } else { //切换店铺
            wx.setStorageSync('shopId', this.data.storeList[index].id); //保存店铺id
            app.globalData.isRefreshStore = true; //刷新店铺标识
            app.globalData.isRefreshCategory = true; //刷新分类标识
            
            wx.showToast({
                title: '店铺切换成功！',
                icon: 'success'
            });
            setTimeout(() => {
                wx.navigateBack(-1);
            }, 1000);
        }
    },

    clearChoose() { //清除已选店铺
        let list = this.data.storeList;
        list.forEach(item => {
            if (item.id == this.data.storeSelected.id) {
                item.selected = false;
            }
        });
        this.setData({
            storeList: list,
            storeSelected: ''
        });

        wx.removeStorageSync('shopId'); //清除店铺id
        app.globalData.isRefreshStore = true; //刷新店铺标识
        app.globalData.isRefreshCategory = true; //刷新分类标识
    },

    onPullDownRefresh() {
        //下拉刷新
        this.setData({
            storeList: [],
            pageNum: 1,
            total: 0,
            isFinished: false,
        });
        this.getStoreData();
    },
    onReachBottom() {
        //加载更多
        if(!this.data.isFinished) {
            this.setData({
                pageNum: this.data.pageNum+1,
            });
            
            setTimeout(() => {
                this.getStoreData();
            }, 800);
        }
    },
})